<?php
/**
 * 后台管理
 * @author jianhui
 *
 */
class Admin extends MY_Controller{
    function Admin(){
        parent::MY_Controller();
        if (!$this->auth->isImport()){
            showError('您无此页面操作权限','/');
        }
        $tab = $this->input->get('tab');
        $this->viewData['tab'] = $tab ? $tab : 'index';
        //主题
        //$this->theme = 'bootstrap';
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('Admin', 'index'),'title'=>'管理首页');
        //标签菜单
        $this->navbar = array(
            //array('title'=>'管理端首页','url'=>printUrl('Admin','index'),'focusTag'=>'index'),
            array('title'=>'时段配置','url'=>printUrl('Admin','timeUnitList'),'focusTag'=>'timeUnitList'),
        );
        $this->viewData['focusTag'] = $this->input->get('m') ? $this->input->get('m') : 'index';
    }
    /**
     * 首页
     */
    function index(){
        $this->load->view('Admin/index', $this->viewData);
    }
    /**
     * 时段列表
     */
    function timeUnitList(){
        $this->navbarFocus = 'AdminProType_index';
        //年份
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $where = array();
        $year = $this->input->get('year');
        $where['year'] = $year ? $year : $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时段列表
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getList($where, '', '', '', 'orderNum asc,id asc');
        $this->load->view('Admin/timeUnitList', $this->viewData);
    }
    /**
     * 增加或修改时段
     */
    function timeUnitAdd(){
        $this->theme = 'bootstrap';
        if ($_POST){
            $id = $this->input->post('id');
            $data = array(
                'year'  => (int)$this->input->post('year'),
                'name'  => $this->input->post('name'),
                'orderNum'  => (int)$this->input->post('orderNum'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'weekNum' => (int)$this->input->post('weekNum'),
                'timeSetting' => serialize(array()),
            );
            $data['year'] || showError('请输入年份');
            $data['year'] >= 2016 || showError('年份应大于或等于2016年');
            $data['name'] || showError('请输入时段名称');
            $data['orderNum'] || $data['orderNum'] = 0;
            $data['startTime'] || showError('请选择时段开始时间');
            $data['endTime'] || showError('请选择时段结束时间');
            date('d', strtotime($data['startTime'])) == 1 || showError('开始时间非当月第一天');
            //结束时间戳
            $endTime = strtotime($data['endTime']);
            date('d', $endTime) == date('t', $endTime) || showError('结束时间非当月最后一天');
            //周数
            $data['weekNum'] > 0 || showError('双月周数必须大于0');
            $weekList = $this->timeUnitModel->getWeekNumList($data['weekNum']);
            $data['weekList'] = serialize($weekList);
            if ($this->timeUnitModel->getData(array('year'=>$data['year'],'name'=>$data['name'],'id !='=>$id))){
                showError('已存在同名时段');
            }
            if ($id){
                $timeId = $this->timeUnitModel->edit($id, $data);
            }else{
                $timeId = $this->timeUnitModel->add($data);
            }
            if ($timeId){
                $this->timeUnitModel->createTimeUnitCache();
                showSuccess('时段保存成功');
            }else{
                showError('时段保存失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $this->viewData['timeUnitData'] = $this->timeUnitModel->getNewData($id);
                $this->viewData['timeUnitData'] || showError('数据不存在');
            }
            $this->load->view('Admin/timeUnitAdd', $this->viewData);
        }
    }
}